const bcrypt = require('bcryptjs')      //密码加密、解密  
// 配置user表
const { Sequelize, Model } = require('sequelize')
const { sequelize } = require('../../config/db')
class User extends Model{

}
User.init({ 
  // id:{
  //   type: Sequelize.INTEGER,
  //   primaryKey: true,   //设置为主键
  //   autoIncrement: true,    //主键递增
  // },
  password: {
    type: Sequelize.STRING,
    set(val){
      const salt = bcrypt.genSaltSync(10) //获取盐  参数：数字越大，服务器成本越高，安全性越高
      // 生成密码
      const password = bcrypt.hashSync(val,salt)
      this.setDataValue('password',password)
    }
  },

  
  email:{
    type: Sequelize.STRING(128), //限制长度
    unique: true,     //是否唯一
  },
  nickname: Sequelize.STRING,
  avatarUrl: Sequelize.STRING,
},{
  sequelize,
  tableName: 'user'
})
module.exports = {
  User
}